﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;


namespace QuanLyDaiLyVersion2.XuLyDuLieu
{
    public class DataAccess
    {
        protected static string _connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=E:\\si document\\cntt\\nhap mon cong nghe phan mem\\do an\\Nhom 18\\QuanLyDaiLyVersion2\\QuanLyDaiLyVersion2\\data\\QuanLyDaiLy.mdb";
        protected static OleDbConnection _connection = null;
        public static void OpenConnection()
        {
            try
            {
                _connection = new OleDbConnection(_connectionString);
                _connection.Open();
            }
            catch
            {
                MessageBox.Show("Không kết nối được CSDL", "Thông báo lỗi");
            }
        }

        public static void CloseConnection()
        {
            if (_connection != null)
            {
                _connection.Close();
            }
        }
        public static DataTable ExecQuery(string s)
        {
            OleDbDataAdapter adapter = new OleDbDataAdapter(s, _connectionString);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            return dt;
        }
        public static void ExecNonQuery(string s)
        {
            OpenConnection();
            OleDbCommand cmd = new OleDbCommand(s, _connection);
            cmd.ExecuteNonQuery();
            CloseConnection();
        }

        public static void update(DataTable table, string query)
        {
            OpenConnection();
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            adapter.SelectCommand = new OleDbCommand(query,_connection);
            OleDbCommandBuilder builder= new OleDbCommandBuilder(adapter);
            adapter.Update(table);
            CloseConnection();
            
        }
    }
}
